<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

require_once("../basicInfo/db.php");

class Question {
    private $questId;
    private $userId;
    private $courseId;
    private $qDate;
    private $vId;
    private $vDate;
    private $qType;
    private $level;
    private $title;
	private $topic;
    private $question;
    private $status;
    private $gold;
    private $first;
    private $second;
    private $third;
    private $fourth;
    private $answer;
	private $timeLimit;
     private $tid;
    public function getQuestId() {
        return $this->questId;
    }

    public function getUserId() {
        return $this->userId;
    }

    public function getCourseId() {
        return $this->courseId;
    }

    public function getQDate() {
        return $this->qDate;
    }

    public function getVId() {
        return $this->vId;
    }

    public function getVDate() {
        return $this->vDate;
    }

    public function getQType() {
        return $this->qType;
    }

    public function getLevel() {
        return $this->level;
    }

    public function getTitle() {
        return $this->title;
    }

	public function getTopic() {
        return $this->topic;
    }

    public function getQuestion() {
        return $this->question;
    }

    public function getStatus() {
        return $this->status;
    }

    public function getGold() {
        return $this->gold;
    }

    public function getFirst() {
        return $this->first;
    }

    public function getSecond() {
        return $this->second;
    }

    public function getThird() {
        return $this->third;
    }

    public function getFourth() {
        return $this->fourth;
    }

    public function getAnswer() {
        return $this->answer;
    }

	public function getTime() {
        return $this->timeLimit;
    }
	public function getTid() {
        return $this->tid;
    }

    public function setQuestId($questionID) {
        $this->questId = $questionID;
    }

    public function setUserId($userID) {
        $this->userId = $userID;
    }

    public function setCourseId($courseID) {
        $this->courseId = $courseID;
    }

    public function setQDate($QuestionDate) {
        $this->qDate = $QuestionDate;
    }

    public function setVId($validatorID) {
        $this->vId = $validatorID;
    }

    public function setVDate($validationDate) {
        $this->vDate = $validationDate;
    }

    public function setQType($questionType) {
        $this->qType = $questionType;
    }

    public function setLevel($questionLevel) {
        $this->level = $questionLevel;
    }

    public function setTitle($questionTitle) {
        $this->title = $questionTitle;
    }

	public function setTopic($questionTopic) {
        $this->topic = $questionTopic;
    }

    public function setQuestion($questionText) {
        $this->question = $questionText;
    }

    public function setStatus($questionStatus) {
        $this->status = $questionStatus;
    }

    public function setGold($questionGold) {
        $this->gold = $questionGold;
    }

    public function setFirst($optionFirst) {
        $this->first = $optionFirst;
    }

    public function setSecond($optionSecond) {
        $this->second = $optionSecond;
    }

    public function setThird($optionThird) {
        $this->third = $optionThird;
    }

    public function setFourth($optionFourth) {
        $this->fourth = $optionFourth;
    }

    public function setAnswer($correctAnswer) {
        $this->answer = $correctAnswer;
    }

    public function setTime($timet) {
        $this->timeLimit = $timet;
    }

	public function setTid($topicid) {
        $this->tid = $topicid;
    }

}
   /**
    * controller class for questionss.
    *
    * @todo methods need implementation
    */
   class QuestionController {
   /* MHS: format todo to conform to phpdocu standard
    * MHS: todo should indicate what needs to be done
    * MHS: basic planned functionality should be included in todo header
    */
       /**
        * Constructor for question controller
        * @todo to be written
        * @return
        */
       public function __construct() {

       }

       /**
        * Returns an instantiation of a new question object.
        * @return Question instatiation of a question object
        */
       public static function getQuestion() {
           return new Question();
       }

       /**
        * Gets a list of questions of a user.
        * The list is sorted by date.
        * @param int $userID user ID
        * @return array of Question objects sorted by date
        */
       public static function getUserQuestions($userId) {
       $quesDataArr = getUserQuestions($userId);
           $quesObjArr = array();

           for ($i = 0; $i < sizeof($quesDataArr); $i++) {
               $quesObj = QuestionController::getQuestion();

               $quesObj->setQuestId($quesDataArr[$i]['qid']);
               $quesObj->setUserId($quesDataArr[$i]['uid']);
               //$quesObj->setCourseId($quesDataArr[$i]['cid']);
               $quesObj->setQDate($quesDataArr[$i]['date']);
               $quesObj->setVId($quesDataArr[$i]['vid']);
               $quesObj->setVDate($quesDataArr[$i]['vdate']);
               $quesObj->setQType($quesDataArr[$i]['type']);
               $quesObj->setLevel($quesDataArr[$i]['level']);
               $quesObj->setTitle($quesDataArr[$i]['title']);
               $quesObj->setQuestion($quesDataArr[$i]['question']);
               $quesObj->setStatus($quesDataArr[$i]['status']);
               $quesObj->setGold($quesDataArr[$i]['gold']);
               $quesObj->setFirst($quesDataArr[$i]['first']);
               $quesObj->setSecond($quesDataArr[$i]['second']);
               $quesObj->setThird($quesDataArr[$i]['third']);
               $quesObj->setFourth($quesDataArr[$i]['fourth']);
               $quesObj->setAnswer($quesDataArr[$i]['answer']);
               $quesObj->setTime($quesDataArr[$i]['timelimit']);
               //$quesObj->setTopic($quesDataArr[$i]['topic']);
               $quesObj->setTid($quesDataArr[$i]['tid']);

               array_push($quesObjArr, $quesObj);
           }

           return $quesObjArr;

   }

   public static function getUserPendingQuestions($userId) {
       //echo "user id is: ". $userId;
       $quesDataArr = getUserPendingQuestions($userId);
           $quesObjArr = array();

           for ($i = 0; $i < sizeof($quesDataArr); $i++) {
               $quesObj = QuestionController::getQuestion();

               $quesObj->setQuestId($quesDataArr[$i]['qid']);
               $quesObj->setUserId($quesDataArr[$i]['uid']);
               //$quesObj->setCourseId($quesDataArr[$i]['cid']);
               $quesObj->setQDate($quesDataArr[$i]['date']);
               $quesObj->setVId($quesDataArr[$i]['vid']);
               $quesObj->setVDate($quesDataArr[$i]['vdate']);
               $quesObj->setQType($quesDataArr[$i]['type']);
               $quesObj->setLevel($quesDataArr[$i]['level']);
               $quesObj->setTitle($quesDataArr[$i]['title']);
               $quesObj->setQuestion($quesDataArr[$i]['question']);
               $quesObj->setStatus($quesDataArr[$i]['status']);
               $quesObj->setGold($quesDataArr[$i]['gold']);
               $quesObj->setFirst($quesDataArr[$i]['first']);
               $quesObj->setSecond($quesDataArr[$i]['second']);
               $quesObj->setThird($quesDataArr[$i]['third']);
               $quesObj->setFourth($quesDataArr[$i]['fourth']);
               $quesObj->setAnswer($quesDataArr[$i]['answer']);
               $quesObj->setTime($quesDataArr[$i]['timelimit']);
               //$quesObj->setTopic($quesDataArr[$i]['topic']);
               $quesObj->setTid($quesDataArr[$i]['tid']);

               array_push($quesObjArr, $quesObj);
           }
           //echo "size of the array is: ". sizeof($quesDataArr);
           //echo "array value is". $quesDataArr[0][1];
           //echo "size of quesObj array is ". sizeof($quesObjArr);
           return $quesObjArr;

   }

   public static function getUserRejectedQuestions($userId) {
       $quesDataArr = getUserRejectedQuestions($userId);
           $quesObjArr = array();

           for ($i = 0; $i < sizeof($quesDataArr); $i++) {
               $quesObj = QuestionController::getQuestion();

               $quesObj->setQuestId($quesDataArr[$i]['qid']);
               $quesObj->setUserId($quesDataArr[$i]['uid']);
               //$quesObj->setCourseId($quesDataArr[$i]['cid']);
               $quesObj->setQDate($quesDataArr[$i]['date']);
               $quesObj->setVId($quesDataArr[$i]['vid']);
               $quesObj->setVDate($quesDataArr[$i]['vdate']);
               $quesObj->setQType($quesDataArr[$i]['type']);
               $quesObj->setLevel($quesDataArr[$i]['level']);
               $quesObj->setTitle($quesDataArr[$i]['title']);
               $quesObj->setQuestion($quesDataArr[$i]['question']);
               $quesObj->setStatus($quesDataArr[$i]['status']);
               $quesObj->setGold($quesDataArr[$i]['gold']);
               $quesObj->setFirst($quesDataArr[$i]['first']);
               $quesObj->setSecond($quesDataArr[$i]['second']);
               $quesObj->setThird($quesDataArr[$i]['third']);
               $quesObj->setFourth($quesDataArr[$i]['fourth']);
               $quesObj->setAnswer($quesDataArr[$i]['answer']);
               $quesObj->setTime($quesDataArr[$i]['timelimit']);
               //$quesObj->setTopic($quesDataArr[$i]['topic']);
               $quesObj->setTid($quesDataArr[$i]['tid']);

               array_push($quesObjArr, $quesObj);
           }

           return $quesObjArr;

   }
   public static function getUserApprovedQuestions($userId) {
       $quesDataArr = getUserApprovedQuestions($userId);
           $quesObjArr = array();

           for ($i = 0; $i < sizeof($quesDataArr); $i++) {
               $quesObj = QuestionController::getQuestion();

               $quesObj->setQuestId($quesDataArr[$i]['qid']);
               $quesObj->setUserId($quesDataArr[$i]['uid']);
               //$quesObj->setCourseId($quesDataArr[$i]['cid']);
               $quesObj->setQDate($quesDataArr[$i]['date']);
               $quesObj->setVId($quesDataArr[$i]['vid']);
               $quesObj->setVDate($quesDataArr[$i]['vdate']);
               $quesObj->setQType($quesDataArr[$i]['type']);
               $quesObj->setLevel($quesDataArr[$i]['level']);
               $quesObj->setTitle($quesDataArr[$i]['title']);
               $quesObj->setQuestion($quesDataArr[$i]['question']);
               $quesObj->setStatus($quesDataArr[$i]['status']);
               $quesObj->setGold($quesDataArr[$i]['gold']);
               $quesObj->setFirst($quesDataArr[$i]['first']);
               $quesObj->setSecond($quesDataArr[$i]['second']);
               $quesObj->setThird($quesDataArr[$i]['third']);
               $quesObj->setFourth($quesDataArr[$i]['fourth']);
               $quesObj->setAnswer($quesDataArr[$i]['answer']);
               $quesObj->setTime($quesDataArr[$i]['timelimit']);
               //$quesObj->setTopic($quesDataArr[$i]['topic']);
               $quesObj->setTid($quesDataArr[$i]['tid']);

               array_push($quesObjArr, $quesObj);
           }

           return $quesObjArr;

   }

   public static function makeQuestion($uid, $tid, $title, $level, $question, $gold, $first, $second, $third, $fourth, $answer, $timelimit){
       $type="";
       if($third !=""){
           $type="MC";
       }else{$type="TF" ;}

      if($third=="" || $third=="NULL"){
          $third="NULL";
      }
      if($fourth=="" || $fourth=="NULL"){
          $fourth="NULL";
      }

     $result= makeQuestion1($uid, $tid, $title, $type, $level, $question, $gold, $first, $second, $third, $fourth, $answer, $timelimit);
     return $result;
   }

   public static function getThisQuestion1($quesID){
       $quesData=getThisQuestion($quesID);
       $quesObj = QuestionController::getQuestion();

               $quesObj->setQuestId($quesData['qid']);
               $quesObj->setUserId($quesData['uid']);
               //$quesObj->setCourseId($quesData['cid']);
               $quesObj->setQDate($quesData['date']);
               $quesObj->setVId($quesData['vid']);
               $quesObj->setVDate($quesData['vdate']);
               $quesObj->setQType($quesData['type']);
               $quesObj->setLevel($quesData['level']);
               $quesObj->setTitle($quesData['title']);
               $quesObj->setQuestion($quesData['question']);
               $quesObj->setStatus($quesData['status']);
               $quesObj->setGold($quesData['gold']);
               $quesObj->setFirst($quesData['first']);
               $quesObj->setSecond($quesData['second']);
               $quesObj->setThird($quesData['third']);
               $quesObj->setFourth($quesData['fourth']);
               $quesObj->setAnswer($quesData['answer']);
               $quesObj->setTime($quesData['timelimit']);
               //$quesObj->setTopic($quesData['topic']);
               $quesObj->setTid($quesData['tid']);

               return $quesObj;


   }

   public static function editQuestion($qid, $tid, $title, $level, $question, $gold, $first, $second, $third, $fourth, $answer, $timelimit){
       $type="";
       if($third !=""){
           $type="MC";
       }else{$type="TF" ;}

      if($third=="" || $third=="NULL"){
          $third="NULL";
      }
      if($fourth=="" || $fourth=="NULL"){
          $fourth="NULL";
      }

     $result= editQuestion($qid, $tid, $title, $type, $level, $question, $gold, $first, $second, $third, $fourth, $answer, $timelimit);

     return $result;
   }

   public static function getQuestionCourse($tid){
   //echo "tid is:". $tid ;
   $questionSid=getQuestionSid($tid);
   //echo "question sid is". $questionSid;
   $courseDataArr = getCourseSection($questionSid);
   //echo "course is: ". $courseDataArr;
   //$courseObj = CourseController::getCourse();
   //$courseObj->setSection($courseDataArr['section']);
   //$courseObj->setCId($courseDataArr['course']);
   $questionCourse=$courseDataArr['course']."-".$courseDataArr['section'];
   //echo "course & section is::".$questionCourse=$courseDataArr['course']."-".$courseDataArr['section'];
   //return $courseObj;
   return $questionCourse;
   }

   public static function getQuestionSID($tid){
       return $questionSid=getQuestionSid($tid);

   }

   public static function getQuestionTopic($tid){
   $questionTopic=getQuestionTopic($tid);
   return $questionTopic;

}
    /**
     * Gets a list of pending questions for the user.
     * The list is sorted by date.
     * @param $status
     * @return array of Question objects with status 'Pending' and sorted by date
     */

     public static function getPendingQuestions() {
		$pendingQuesArr = getPendingQuestions();
        $pendingQuesObjArr = array();

        for ($i = 0; $i < sizeof($pendingQuesArr); $i++) {
            $quesObj = QuestionController::getQuestion();

            $quesObj->setQuestId($pendingQuesArr[$i]['qid']);
			$quesObj->setQuestion($pendingQuesArr[$i]['question']);
            $quesObj->setLevel($pendingQuesArr[$i]['level']);
            $quesObj->setQType($pendingQuesArr[$i]['type']);
			$quesObj->setTime($pendingQuesArr[$i]['timelimit']);
            $quesObj->setQDate($pendingQuesArr[$i]['date']);
			$quesObj->setTid($pendingQuesArr[$i]['tid']);
			$quesObj->setUserId($pendingQuesArr[$i]['uid']);



            array_push($pendingQuesObjArr, $quesObj);
        }

        return $pendingQuesObjArr;

	}



	public static function approveQuestionV($vid,$ts,$qid,$uid,$gold) {
		$result=updateApprovedQuestionV($vid,$ts,$qid,$uid,$gold);
		return $result;
	}

	public static function rejectQuestionV($vid,$ts,$qid) {
		$result=updateRejectedQuestionV($vid,$ts,$qid);
		return $result;
	}

	public static function getQuestionInfo($qid) {
		$questioninfo = getQuestionInfo($qid);

		$questioninfoObj = array();

        for ($i = 0; $i < sizeof($questioninfo); $i++) {
            $quesObj = QuestionController::getQuestion();

            $quesObj->setQuestId($questioninfo[$i]['qid']);
			$quesObj->setQuestion($questioninfo[$i]['question']);
			$quesObj->setFirst($questioninfo[$i]['first']);
			$quesObj->setSecond($questioninfo[$i]['second']);
			$quesObj->setThird($questioninfo[$i]['third']);
			$quesObj->setFourth($questioninfo[$i]['fourth']);
			$quesObj->setAnswer($questioninfo[$i]['answer']);
			$quesObj->setTid($questioninfo[$i]['tid']);
			$quesObj->setLevel($questioninfo[$i]['level']);
            $quesObj->setQType($questioninfo[$i]['type']);
			$quesObj->setTime($questioninfo[$i]['timelimit']);
            $quesObj->setQDate($questioninfo[$i]['date']);
			$quesObj->setStatus($questioninfo[$i]['status']);
			$quesObj->setGold($questioninfo[$i]['gold']);

            array_push($questioninfoObj, $quesObj);
        }

        return $questioninfoObj;

    }

    public static function getQuestionsCL($course,$level)
	    {
	        $questioninfo = getQuestionsCL($course,$level);

			$questioninfoObj = array();

	        for ($i = 0; $i < sizeof($questioninfo); $i++) {
	            $quesObj = QuestionController::getQuestion();

	                        $quesObj->setQuestId($questioninfo[$i]['qid']);
				$quesObj->setQuestion($questioninfo[$i]['question']);
				$quesObj->setFirst($questioninfo[$i]['first']);
				$quesObj->setSecond($questioninfo[$i]['second']);
				$quesObj->setThird($questioninfo[$i]['third']);
				$quesObj->setFourth($questioninfo[$i]['fourth']);
				$quesObj->setAnswer($questioninfo[$i]['answer']);
				$quesObj->setGold($questioninfo[$i]['gold']);
				$quesObj->setQType($questioninfo[$i]['type']);
	                        $quesObj->setTime($questioninfo[$i]['timelimit']);
	            array_push($questioninfoObj, $quesObj);
	        }

	        return $questioninfoObj;

    }


}



?>
